package com.endpoint.member.controller;

import com.endpoint.common.bean.UserDetails;
import com.endpoint.common.constant.ResponseStatus;
import com.endpoint.common.utils.ResultBean;
import com.endpoint.common.utils.SMSCodeUtils;
import com.endpoint.member.constant.MemberConstant;
import com.endpoint.member.feign.ThirdPartyFeignService;
import com.endpoint.member.service.MemberRegisterService;
import com.endpoint.member.vo.MemberRegisterVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Project: endpoint
 * @Name: MemberRegisterController
 * @Author: Jack_Li
 * @Date: 2024/7/5 19:01
 */
@Api(tags = "登录相关接口")
@RestController
@RequestMapping("/member/register")
public class MemberRegisterController {

    @Resource
    private ThirdPartyFeignService thirdPartyFeignService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private MemberRegisterService registerService;

    @GetMapping("/sms/sendCode")
    public ResultBean sendCode(@RequestParam("phone") String phone){
        // 从redis获取保存的验证码
        String redisCode = stringRedisTemplate.opsForValue().get(MemberConstant.SMS_CODE_CACHE_PREFIX + phone);
        if (!StringUtils.isEmpty(redisCode)) {
            // 如果验证码已经存在,判断该验证码存入的时间与当前时间是否小于60秒
            if (System.currentTimeMillis() - Long.parseLong(redisCode.split("_")[1]) < 60000) {
                // 小于60秒不能再发
                return ResultBean.fail(ResponseStatus.SMS_CODE_EXCEPTION);
            }
        }
        String code = SMSCodeUtils.randomCode();
        stringRedisTemplate.opsForValue()
                .set(MemberConstant.SMS_CODE_CACHE_PREFIX + phone,
                        code+"_"+System.currentTimeMillis(),10, TimeUnit.MINUTES);
        ResultBean resultBean = thirdPartyFeignService.sendCode(phone, code);
        if (resultBean.getCode() == 200){
            return ResultBean.ok();
        }
        return ResultBean.error();
    }

    @ApiOperation(value = "用户注册")
    @PostMapping
    public ResultBean register(@Valid @RequestBody MemberRegisterVo registerVo, BindingResult result){
        // 将传递过来的验证码 与 存redis中的验证码进行比较
        String code = registerVo.getVelCode();
        String value = stringRedisTemplate.opsForValue().get(MemberConstant.SMS_CODE_CACHE_PREFIX + registerVo.getUsername());
        //如果该手机号已经发送验证码并且没有过期
        if (!StringUtils.isEmpty(value)) {
            // 验证码和redis中的一致
            if(code.equals(value.split("_")[0])) {
                // 校验成功删除验证码：令牌机制
                stringRedisTemplate.delete(MemberConstant.SMS_CODE_CACHE_PREFIX + registerVo.getUsername());
                // 执行注册功能
                UserDetails userDetails = registerService.register(registerVo);
                return ResultBean.ok(userDetails);
            }else{
                //验证码与Redis中不一致
                return ResultBean.fail(ResponseStatus.VEL_CODE_ERROR);
            }
        }else{
            //验证码已过期,需要重新发送
            return ResultBean.fail(ResponseStatus.VEL_CODE_ERROR);
        }
    }

}
